import { EventTarget } from 'cc';

/**
 * 全局事件管理器
 * 支持类型安全的事件监听和派发
 */
export class GlobalEventManager {
    private static instance: GlobalEventManager;
    private eventTarget: EventTarget;
    private eventCallbacks: Map<string, Set<Function>>;

    private constructor() {
        this.eventTarget = new EventTarget();
        this.eventCallbacks = new Map();
    }

    /**
     * 获取单例实例
     */
    public static getInstance(): GlobalEventManager {
        if (!GlobalEventManager.instance) {
            GlobalEventManager.instance = new GlobalEventManager();
        }
        return GlobalEventManager.instance;
    }

    /**
     * 监听事件
     * @param event 事件名称
     * @param callback 回调函数
     * @param target 目标对象（用于移除监听）
     */
    on<T = any>(event: string, callback: (data: T) => void, target?: any): void {
        this.eventTarget.on(event, callback, target);
        this.trackCallback(event, callback);
    }

    /**
     * 监听一次性事件
     * @param event 事件名称
     * @param callback 回调函数
     * @param target 目标对象
     */
    once<T = any>(event: string, callback: (data: T) => void, target?: any): void {
        this.eventTarget.once(event, callback, target);
        this.trackCallback(event, callback);
    }

    /**
     * 派发事件
     * @param event 事件名称
     * @param data 事件数据
     */
    emit<T = any>(event: string, data?: T): void {
        this.eventTarget.emit(event, data);
    }

    /**
     * 移除事件监听
     * @param event 事件名称
     * @param callback 回调函数
     * @param target 目标对象
     */
    // off(event: string, callback?: Function, target?: any): void {
    //     this.eventTarget.off(event, callback, target);
    //     if (callback) {
    //         this.untrackCallback(event, callback);
    //     }
    // }

    /**
     * 移除目标对象的所有事件监听
     * @param target 目标对象
     */
    removeAll(target: any): void {
        this.eventTarget.removeAll(target);
        this.removeAllCallbacksForTarget(target);
    }

    /**
     * 检查事件是否有监听器
     * @param event 事件名称
     */
    hasListener(event: string): boolean {
        const callbacks = this.eventCallbacks.get(event);
        return callbacks ? callbacks.size > 0 : false;
    }

    /**
     * 获取事件监听器数量
     * @param event 事件名称
     */
    getListenerCount(event: string): number {
        const callbacks = this.eventCallbacks.get(event);
        return callbacks ? callbacks.size : 0;
    }

    /**
     * 清空所有事件监听
     */
    // clearAll(): void {
    //     this.eventCallbacks.forEach((callbacks, event) => {
    //         callbacks.forEach(callback => {
    //             this.eventTarget.off(event, callback);
    //         });
    //     });
    //     this.eventCallbacks.clear();
    // }

    /**
     * 跟踪回调函数
     */
    private trackCallback(event: string, callback: Function): void {
        if (!this.eventCallbacks.has(event)) {
            this.eventCallbacks.set(event, new Set());
        }
        this.eventCallbacks.get(event)!.add(callback);
    }

    /**
     * 取消跟踪回调函数
     */
    private untrackCallback(event: string, callback: Function): void {
        const callbacks = this.eventCallbacks.get(event);
        if (callbacks) {
            callbacks.delete(callback);
            if (callbacks.size === 0) {
                this.eventCallbacks.delete(event);
            }
        }
    }

    /**
     * 移除目标对象的所有回调跟踪
     */
    private removeAllCallbacksForTarget(target: any): void {
        // 这里需要更复杂的逻辑来跟踪target对应的回调
        // 简化版本：清空所有跟踪（实际项目中需要更精细的管理）
        this.eventCallbacks.clear();
    }
}

// 导出单例实例
export const eventManager = GlobalEventManager.getInstance();